package net.quindu.android.smsfilter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.TabActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SmsFilter extends TabActivity {
	
	
	
	ArrayAdapter<MenuElement> inboxList;
	ArrayAdapter<MenuElement> filterList;
	SQLiteDatabase db;
	TabHost mTabHost;
	ListView folderView;
	ListView filterView;
	protected static final int SUB_ACTIVITY_FOLDER_DETAIL = 100;
	
	protected static final int SUB_ACTIVITY_FILTER_DETAIL = 200;
	protected static final int SUB_ACTIVITY_FILTER_CONDITIONS = 201;
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        
        
        setContentView(R.layout.main);

        mTabHost = getTabHost();
        
        //mTabHost.addTab(mTabHost.newTabSpec("tabFolders").setIndicator("Folders").setContent(R.id.folderView));
        mTabHost.addTab(mTabHost.newTabSpec("tabFilters").setIndicator("Filters").setContent(R.id.filterView));
        
        mTabHost.setCurrentTab(0);        
        findViewById(R.id.folderView).setVisibility(View.GONE);
        //-
        
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        db = dbHelper.getReadableDatabase();
        
        
        // ==========================
        // folders
        
        folderView = (ListView) findViewById(R.id.folderView);
        
        populateFoldersListItems();
        folderView.setAdapter(inboxList);
        
    
        folderView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {		
				
				Intent i = new Intent(SmsFilter.this,FilterActivity.class);
				startActivityForResult(i, SUB_ACTIVITY_FOLDER_DETAIL);
			
			}
          });
        
        
        // ==========================
        // filters
        
        filterView = (ListView) findViewById(R.id.filterView);
        filterList = new ArrayAdapter<MenuElement>(this, android.R.layout.simple_list_item_1);
        filterView.setAdapter(filterList);
        populateFilterListItems();
        
        
        
        
        
        filterView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {		
				
				Intent i = new Intent(SmsFilter.this,FilterActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("id", (int) filterList.getItem(position).getId());
				i.putExtras(bundle);
				startActivityForResult(i, SUB_ACTIVITY_FILTER_DETAIL);			
			}
          });        
        
        /*
        //4 test
		Intent i = new Intent(SmsFilter.this,FilterActivity.class);
		startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);
		*/
        
    }


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
			menu.add(0, 1, 0, "New");
		
		//menu.add(0, 2, 0, "Save");
		//menu.add(0, 3, 0, "Back");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		// folders
		if(mTabHost.getCurrentTab() == 0){
			switch (item.getItemId()) {
			case 1:
				showAddDialog();
			break;
			}
			
		// filters
		}else{
			switch (item.getItemId()) {
			case 1:
				showAddDialog();
			break;
			}
			
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	private void showAddDialog(){
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		final EditText input = new EditText(this);
		alert.setView(input);
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString().trim();
				addFolder(value);
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		alert.show();		
	}
	
	private void populateFoldersListItems(){
        Cursor cursor = db.query("sms_folder", new String[] {"folder_id", "title"}, null , null, null, null, null);
        inboxList = new ArrayAdapter<MenuElement>(this, android.R.layout.simple_list_item_1);
        while(cursor.moveToNext()){        	        	
        	inboxList.add(new MenuElement(cursor.getString(1), cursor.getInt(0)));        	
        }		
	}
	
	private void populateFilterListItems(){
        Cursor cursor = db.query("sms_filter", new String[] {"filter_id", "title"}, null , null, null, null, null);
    	filterList.clear();
        while(cursor.moveToNext()){        	        	
        	filterList.add(new MenuElement(cursor.getString(1), cursor.getInt(0)));        	
        }
        
        
        filterList.notifyDataSetChanged();               
	}	
	
	private void addFolder(String title){
        ContentValues initialValues = new ContentValues();
        initialValues.put("title", title);        
        db.insert("sms_folder", null, initialValues);
        populateFoldersListItems();        		
	}

	private void showMsg(String msg) {
		Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2);
		toast.show();
	}
	
	
    
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);				 	
		if(requestCode == SUB_ACTIVITY_FILTER_DETAIL){				
				populateFilterListItems();
        }
		
	}	
    
	

}